package com.hyjoy.web.servlet;

import com.hyjoy.entity.PageBean;
import com.hyjoy.entity.Product;
import com.hyjoy.service.ProductService;
import com.hyjoy.service.impl.ProductServiceImpl;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by hyjoy on 2018/4/5.
 */
public class ProductServlet extends BaseServlet {

    public String findByPage(HttpServletRequest request, HttpServletResponse response) {
        int pageNum = 1;

        try {
            pageNum = Integer.parseInt(request.getParameter("pageNumber"));
        } catch (Exception e) {

        }
        int pageSize = 20;
        String cid = request.getParameter("cid");
        // 调用service分页查询商品
        ProductService productService = new ProductServiceImpl();
        try {
            PageBean<Product> bean = productService.findByPage(pageNum, pageSize, cid);
            request.setAttribute("pb", bean);
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("msg", "分页查询失败");
            return "/jsp/ms.jsp";
        }
        return "/jsp/product_list.jsp";
    }

    public String getById(HttpServletRequest request, HttpServletResponse response) {
        // 获取pid
        String pid = request.getParameter("pid");
        ProductService productService = new ProductServiceImpl();
        try {
            Product product = productService.getById(pid);
            request.setAttribute("bean", product);
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("msg", "查询单个商品失败");
            return "/jsp/msg.jsp";
        }
        return "/jsp/product_info.jsp";
    }
}
